var express = require('express');
var date = require("silly-datetime");
var router = express.Router();
var sqlUtils = require('../util/sqlUtil');
const addUtils = require('../util/addUtils');
/* GET users listing. */
// 查询学校信息|模糊查询
router.get('/', function (req, res, next) {
  var page = req.query.page;
  var limit = req.query.limit;
  if(req.query.searchParams==undefined){
    var sql = "select * from school where is_del=0 limit " + limit;
    sqlUtils.sqlConnect(sql, function (err, data) {
      data = JSON.parse(JSON.stringify(data));
      data = {
        "code": 0,
        "msg": "",
        "count": data.length,
        "data": data
      }
      res.send(data);
    })
  }else {
    var result=JSON.parse(req.query.searchParams);
    // console.log(result.year);
    var s_name=(result.s_name.length==0)?'%':result.s_name,
    level=(result.level.length==0)?'%':result.level,
    year=(result.year.length==0)?'%':result.year,
    month = (result.month.length==0)?'%':result.month;
    var time=(month=='%')?year:month;
    var sql="select * from school where is_del=0 and s_name like '%"+s_name+"%' and level like '%"+level+"%' and create_time like '%"+time+"%'";
    sqlUtils.sqlConnect(sql,function (err, data) {
      if (err) {
        console.log(err)
      }else {
        data = JSON.parse(JSON.stringify(data));
        data = {
          "code": 0,
          "msg": "",
          "count": data.length,
          "data": data
        }
        res.send(data);
      }
    })
  }
});
// 修改数据
router.put('/',function(req,res,next){
  var s_name=req.body.username,
  id=req.body.sid;
  var sql="update school set s_name=? where id=?",
  sqlArr=[s_name,id];
  sqlUtils.sqlConnect(sql,sqlArr,function(err){
    if (err) {
      var json = {
        "code": 200,
        "msg": "删除失败"
      };
      console.log(err);
      res.send(json);
    } else {
      var json = {
        "code": 200,
        "msg": "删除成功"
      };
      res.send(json);
    }
  })
})
// 删除学校信息
router.delete("/", function (req, res, next) {
  var str = req.body.id;
  var sql = "update school set is_del=1 where id in(" + str + ")";
  sqlUtils.sqlConnect(sql, function (err, data) {
    if (err) {
      var json = {
        "code": 200,
        "msg": "删除失败"
      };
      res.send(json);
    } else {
      var json = {
        "code": 200,
        "msg": "删除成功"
      };
      res.send(json);
    }
  })
})
// 添加学校信息
router.post("/", function (req, res, next) {
  var s_name = req.body.username;
  var level = req.body.level;
  var grade = req.body.grade;
  var classnum = req.body.class;
  var time = date.format(new Date(), 'YYYY-MM-DD');
  var sql = "insert into school (s_name,is_del,level,create_time) values(?,0,?,?)";
  var sqlArr = [s_name, level, time, s_name];
  sqlUtils.sqlConnect(sql, sqlArr, function (err, data) {
    if (err) {
      console.log(err);
    } else {
      data = JSON.parse(JSON.stringify(data));
      // 当年级选择是小学时
      if (grade == "l1") {
        var grades = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级"];
        addUtils.addMessage(grades, grade, classnum, data.insertId);
      } else if (grade == "l2") { //当年级数选择为初一~初三
        var grades = ["七年级", "八年级", "九年级"];
        addUtils.addMessage(grades, grade, classnum, data.insertId);
      } else if (grade == "l3") { //当年级数选择为初一~初三
        var grades = ["高一", "高二", "高三"];
        addUtils.addMessage(grades, grade, classnum, data.insertId);
      } else if (grade == "l4") { //当年级数选择为初一~初三
        var grades = ["七年级", "八年级", "九年级", "高一", "高二", "高三"];
        addUtils.addMessage(grades, grade, classnum, data.insertId);
      }
      var json = {
        "code": 200,
        "msg": "添加成功"
      };
      res.send(json);
    }
  })
  
})
module.exports = router;